﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChatDBAccess;

namespace Twitter
{
    public partial class DefaultTwitterLayout : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            var channels = Search.AllChannels();
            foreach (Channel chan in channels)
            {
                LinkButton l = new LinkButton();
                l.Text = "#" + chan.Name;
                l.CssClass = "channelLink";
                l.Click += new EventHandler(ShowChannelLog);
                channelsHolder.Controls.Add(l);
            }

            var users = Search.AllUsers();
            foreach (User user in users)
            {
                LinkButton l = new LinkButton();
                l.Text = "@" + user.UserName;
                l.CssClass = "channelLink";
                l.Click += new EventHandler(ShowUserLog);
                userHolder.Controls.Add(l);
            }

            if (Session["userId"] != null)
            {
                Literal lit = new Literal();
                lit.Text = "Wellcome " + (Session["user"] as User).UserName + " | " ;
                userSettings.Controls.Add(lit);
                
                LinkButton msgsBtn = new LinkButton();
                msgsBtn.Text = "My messages";
                msgsBtn.Click += new EventHandler(delegate(object sender1, EventArgs e12) { Page.Response.Redirect("~/MessagesToMe.aspx"); });
                userSettings.Controls.Add(msgsBtn);

                userSettings.Controls.Add(lit);

                LinkButton lb = new LinkButton();
                lb.Text = "Log Out";
                lb.Click += new EventHandler(lb_Click);
                userSettings.Controls.Add(lb);

            }
            else
            {
                LinkButton loginBtn = new LinkButton();
                loginBtn.Text = "Login";
                loginBtn.Click += new EventHandler(delegate(object sender1, EventArgs e12) { Page.Response.Redirect("~/Login.aspx"); });
                LinkButton regBtn = new LinkButton();
                regBtn.Text = "Register";
                regBtn.Click += new EventHandler(delegate(object sender1, EventArgs e12) { Page.Response.Redirect("~/Register.aspx"); });
                
                Literal lit = new Literal();
                lit.Text = " or ";
                userSettings.Controls.Add(loginBtn);
                userSettings.Controls.Add(lit);
                userSettings.Controls.Add(regBtn);
            }
        }

        void lb_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("~/Index.aspx");
        }

        protected void userFilterButton_Click(object sender, EventArgs e)
        {
            var users = Search.SearchUsers(userFilter.Text);
            userHolder.Controls.Clear();
            foreach (User user in users)
            {
                LinkButton l = new LinkButton();
                l.Text = "@" + user.UserName;
                l.CssClass = "channelLink";
                l.Click += new EventHandler(ShowUserLog);
                userHolder.Controls.Add(l);
            }
        }

        protected void channelFilterButton_Click(object sender, EventArgs e)
        {
            var channels = Search.SearchChannels(channelFilter.Text);
            channelsHolder.Controls.Clear();
            foreach (Channel chan in channels)
            {
                LinkButton l = new LinkButton();
                l.Text = "#" + chan.Name;
                l.CssClass = "channelLink";
                l.Click += new EventHandler(ShowChannelLog);
                channelsHolder.Controls.Add(l);
            }
        }

        protected void ShowChannelLog(object sender, EventArgs e)
        {
            string name = ((LinkButton)sender).Text.Substring(1);
            Response.Redirect("~/ViewLogs.aspx?name=" + name);
        }

        protected void ShowUserLog(object sender, EventArgs e)
        {
            string name = ((LinkButton)sender).Text.Substring(1);
            Response.Redirect("~/UserChat.aspx?name=" + name);
        }
    }
}